import type { EntityConfig, FieldConfig } from '@/types/entity'
import { FieldType } from '@/types/entity'

/**
 * 实体配置列表
 * 根据后端生成的实体自动配置
 */
export const entities: EntityConfig[] = [
<#list tables as table>
  {
    name: '${table.className}',
    tableName: '${table.tableName}',
    displayName: '${table.displayName}',
    apiPath: '/api/${table.tableName}',
    tableComment: '${table.comment!""}',
    hasCompositePrimaryKey: ${table.hasCompositePrimaryKey?string("true", "false")},
    fields: [
<#list table.columns as column>
      {
        name: '${column.javaFieldName}',
        type: <#if column.javaType == "String">FieldType.STRING
        <#elseif column.javaType == "Integer">FieldType.INTEGER
        <#elseif column.javaType == "Long">FieldType.LONG
        <#elseif column.javaType == "Double">FieldType.DOUBLE
        <#elseif column.javaType == "BigDecimal">FieldType.BIG_DECIMAL
        <#elseif column.javaType == "Boolean">FieldType.BOOLEAN
        <#elseif column.javaType == "LocalDate">FieldType.LOCAL_DATE
        <#elseif column.javaType == "LocalTime">FieldType.LOCAL_TIME
        <#elseif column.javaType == "LocalDateTime">FieldType.LOCAL_DATE_TIME
        <#else>FieldType.STRING</#if>,
        label: '${column.comment!column.javaFieldName}',
        required: ${column.nullable?string("false", "true")},
        primaryKey: ${column.primaryKey?string("true", "false")},
        autoIncrement: ${column.autoIncrement?string("true", "false")},
        searchable: ${(column.stringType || column.textType)?string("true", "false")},
        sortable: ${(column.primaryKey || column.dateType || column.numberType)?string("true", "false")},
        showInTable: ${(!column.largeTextType)?string("true", "false")},
        showInForm: ${(!column.autoIncrement)?string("true", "false")},
<#if column.stringType>
        maxLength: ${(column.length)!0},
</#if>
<#if column.decimalType>
        scale: ${(column.scale)!0},
</#if>
<#if column.defaultValue??>
        defaultValue: <#if column.javaType == "String">"${column.defaultValue}"<#elseif column.javaType == "Boolean">${column.defaultValue?string("true", "false")}<#elseif column.javaType == "Integer" || column.javaType == "Long">${column.defaultValue}<#else>${column.defaultValue}</#if>,
</#if>
        comment: '${column.comment!""}'
      }<#if column_has_next>,</#if>
</#list>
    ]
  }<#if table_has_next>,</#if>
</#list>
]

/**
 * 添加新实体配置
 * 用于动态添加新的实体配置
 */
export function addEntityConfig(entityConfig: EntityConfig) {
  const existingIndex = entities.findIndex(e => e.name === entityConfig.name)
  if (existingIndex >= 0) {
    entities[existingIndex] = entityConfig
  } else {
    entities.push(entityConfig)
  }
}

/**
 * 获取实体配置
 * 根据表名获取对应的配置
 */
export function getEntityConfig(tableName: string): EntityConfig | undefined {
  return entities.find(entity => entity.tableName === tableName)
}

/**
 * 获取所有实体配置
 * 返回所有已配置的实体列表
 */
export function getAllEntities(): EntityConfig[] {
  return [...entities]
}